﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Resource.AttachedProperty
{
    public class TextBoxWatermarkBehavior
    {
        public static string GetWatermark(DependencyObject obj)
        {
            return (string)obj.GetValue(WatermarkProperty);
        }

        public static void SetWatermark(DependencyObject obj, string value)
        {
            obj.SetValue(WatermarkProperty, value);
        }

        // Using a DependencyProperty as the backing store for Watermark.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxWatermarkBehavior), new PropertyMetadata(null,Init));

        public static void Init(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is TextBox)
            {
                TextBox textBox = d as TextBox;
                textBox.TextChanged += (o, e1) =>
                  {
                      SetShowWatermark(d,string.IsNullOrEmpty(textBox.Text) ? Visibility.Visible:Visibility.Hidden);
                  };
            }
            else if(d is PasswordBox)
            {
                PasswordBox passwordBox = d as PasswordBox;
                passwordBox.PasswordChanged += (o, e1) =>
                  {
                      SetShowWatermark(d, string.IsNullOrEmpty(passwordBox.Password) ? Visibility.Visible : Visibility.Hidden);
                  };
            }
        }



        public static Visibility GetShowWatermark(DependencyObject obj)
        {
            return (Visibility)obj.GetValue(ShowWatermarkProperty);
        }

        private static void SetShowWatermark(DependencyObject obj, Visibility value)
        {
            obj.SetValue(ShowWatermarkProperty, value);
        }

        // Using a DependencyProperty as the backing store for ShowWatermark.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowWatermarkProperty =
            DependencyProperty.RegisterAttached("ShowWatermark", typeof(Visibility), typeof(TextBoxWatermarkBehavior), new PropertyMetadata(Visibility.Visible));




    }
}
